// pages/article/article.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 文章id
    aId: '',
    // 文章详情
    article: '',
    // 评论详情
    commList: "",
    // 评论输入框是否显示
    inputShow: false,
    // 评论输入框内容
    comm_content: "",
    userId: wx.getStorageSync('user').id
  },
  // 获取文章详情
  getArticle() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/articles/' + this.data.aId,
      dataType: '其他',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: function (res) {
        let filterData = res.data.replace(/\"art\_id\"\:\s\d+/g, function (str) {
          return str.replace(/\d+/, function (str2) {
            return '"' + str2 + '"'
          })
        })
        filterData = filterData.replace(/\"aut\_id\"\:\s\d+/g, function (str) {
          return str.replace(/\d+/, function (str2) {
            return '"' + str2 + '"'
          })
        })
        console.log(res)
        res.data = JSON.parse(filterData)
        if (res.statusCode !== 200) return wx.showToast({
          title: '文章加载失败',
          icon: "none"
        })

        this.setData({
          article: res.data.data
        })
      }.bind(this)
    })
  },
  // 获取文章评论
  getComm() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/comments',
      data: {
        type: 'a',
        source: this.data.aId
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      dataType:'其他',
      success: function (res) {
        let filterData = res.data.replace(/\"com\_id\"\:\s\d+/g, function (str) {
          return str.replace(/\d+/, function (str2) {
            return '"' + str2 + '"'
          })
        })
        res.data = JSON.parse(filterData)
        console.log(res)
        if (res.statusCode !== 200) return false
        this.setData({
          commList: res.data.data.results
        })
      }.bind(this)
    })
  },
  // 显示评论输入框
  showCommInput() {
    this.setData({
      inputShow: true
    })
  },
  // 关闭评论输入框
  closeCommInput() {
    this.setData({
      inputShow: false
    })
  },
  // 发送评论
  sendComm() {
    // 判断是否满5个字符
    if (this.data.comm_content.length < 1) {
      return wx.showToast({
        title: '评论不能为空哦~',
        icon: "none"
      })
    }
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/comments',
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        target: this.data.aId,
        content: this.data.comm_content
      },
      success: function (res) {
        console.log(res)
        // 评论失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '评论失败',
          icon: "none"
        })
        // 评论成功
        wx.showToast({
          title: '评论成功',
        })
        this.setData({
          comm_content: ""
        })
        this.getComm()
        this.closeCommInput()
      }.bind(this)
    })
  },
  // 关注文章作者
  attenUser() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followings',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "POST",
      data: {
        target: this.data.article.aut_id
      },
      success: (res) => {
        // 关注失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '关注失败',
          icon: "none"
        })
        // 关注成功
        this.setData({
          'article.is_followed': true
        })
      }
    })
  },
  // 取消关注文章作者
  cancelAtten() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followings/' + this.data.article.aut_id,
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "DELETE",
      success: (res) => {
        // 取消关注失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '关注失败',
          icon: "none"
        })
        // 取消关注成功
        this.setData({
          'article.is_followed': false
        })
      }
    })
  },
  // 添加文章收藏
  addStar() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/collections',
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        target: this.data.article.art_id
      },
      success: (res) => {
        console.log(res)
        // 收藏失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '收藏失败',
          icon: "none"
        })
        // 收藏成功
        this.setData({
          'article.is_collected': true
        })
      }
    })
  },
  // 取消文章收藏
  cancelStar() {

    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/collections/' + this.data.aId,
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        // 取消收藏失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '取消收藏失败',
          icon: "none"
        })
        // 取消收藏成功
        this.setData({
          'article.is_collected': false
        })
      }
    })
  },
  // 点赞
  addLike() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/likings',
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        target: this.data.aId
      },
      success: (res) => {
        console.log(res)
        // 点赞失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '点赞失败',
          icon: "none"
        })
        // 点赞成功
        this.setData({
          'article.attitude': 1
        })
      }
    })
  },
  // 取消点赞
  cancelLike() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/likings/' + this.data.aId,
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        // 取消点赞失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '取消点赞失败',
          icon: "none"
        })
        // 取消点赞成功
        this.setData({
          'article.attitude': -1
        })
      }
    })
  },
  // 不喜欢
  addDislike() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/dislikes',
      method: 'POST',
      data: {
        target: this.data.aId
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        // 不喜欢失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '不喜欢失败',
          icon: "none"
        })
        // 不喜欢成功
        this.setData({
          'article.attitude': 0
        })
      }
    })
  },
  // 取消不喜欢
  cancelDislike() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/article/dislikes/' + this.data.aId,
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        // 取消不喜欢失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '取消不喜欢失败',
          icon: "none"
        })
        // 取消不喜欢成功
        this.setData({
          'article.attitude': -1
        })
      }
    })
  },
  // 添加赞评论
  addCommLike(e) {
    let comm_id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/comment/likings',
      method: 'POST',
      data: {
        target: comm_id
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        // 赞评论失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '点赞失败',
          icon: "none"
        })
        // 赞评论成功
        let index = this.data.commList.findIndex(item => item.com_id == comm_id)
        this.data.commList[index].is_liking = true
        this.data.commList[index].like_count++
        this.setData({
          commList: this.data.commList
        })
      }
    })
  },
  // 取消赞评论
  cancelCommLike(e) {
    let comm_id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/comment/likings/' + comm_id,
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        // 取消赞评论失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '取消点赞失败',
          icon: "none"
        })
        // 取消赞评论成功
        let index = this.data.commList.findIndex(item => item.com_id == comm_id)
        this.data.commList[index].is_liking = false
        this.data.commList[index].like_count--
        this.setData({
          commList: this.data.commList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      aId: options.id
    })
    this.getArticle()
    this.getComm()
  },
  // 解决输入textarea报错的一个方法
  onInput(e) {},
})